
package com.qf.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.qf.entity.Blogs;
import com.qf.service.BlogsService;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;



/**
 * ------回首昨日,更进一步------
 *
 * @author JinXiaoChen
 * @date 2025/5/6
 * @des
 */
@Slf4j
public class BlogsExcelListener extends AnalysisEventListener<Blogs> {
    private static final int BATCH_COUNT = 5;
    private  List<Blogs> list = new ArrayList<>();
    private BlogsService blogsService;

    public BlogsExcelListener(BlogsService blogsService) {
        this.blogsService = blogsService;
    }


    @Override
    public void invoke(Blogs blogs, AnalysisContext analysisContext) {
        System.out.println("解析到一条数据:" + blogs);
        list.add(blogs);
        if (list.size() >= BATCH_COUNT) {
            saveData();
            list.clear();
        }

    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        saveData();
    }
    private void saveData() {
        blogsService.insertBatch(list);
    }
}
